package com.crm.service.impl.cluePoolManager;


import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.dao.mapper.cluePoolManager.ClueContactMapper;
import com.crm.dao.mapper.customMapper.cluePoolManagerCustomMapper.ClueContactCustomMapper;
import com.crm.model.entity.cluePoolManager.ClueContact;
import com.crm.model.entity.cluePoolManager.ClueContactExample;
import com.crm.model.vo.cluePoolManager.ClueContactVO;
import com.crm.service.cluePoolManager.ClueContactDbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @ Author ：wll
 */
@Service
public class ClueContactDbServiceImpl implements ClueContactDbService {

    @Autowired
    private ClueContactCustomMapper clueContactCustomMapper;

    @Autowired
    private ClueContactMapper clueContactMapper;

    /**
     * @author wll
     * info 查询联系人记录
     */
    @Override
    public List<ClueContactVO> selectClueContactVOs(String clueId) {
        List<ClueContactVO> clueContactVOList = clueContactCustomMapper.selectClueContactVOs(clueId);
        return clueContactVOList;
    }

    /**
     * @author wll
     * @date 2021/1/25 15:30
     * info 用户模块-我的线索池-线索清单-新增线索联系人
     * @param clueContact
     * @return
     */
    @Override
    public Boolean insertClueContact(ClueContact clueContact) {
        Boolean result = false;
        int num = clueContactMapper.insertSelective(clueContact);
        if (num > 0) {
            result = true;
        }
        return result;
    }


    /**
     * @author wll
     * @date 2021/1/26 16:32
     * info 用户模块-我的线索池-线索清单-跟进-线索联系人回显
     * @param clueId 线索id
     * @param param  为空查询线索全部联系人和手机号 不为空模糊搜索联系人和手机号
     * @return
     */
    @Override
    public List<ClueContactVO> selectClueContacts(String clueId, String param) {
        return clueContactCustomMapper.selectClueContacts(clueId,param);
    }


    /**
     * @author wll
     * info  添加线索联系人 返回主键
     */
    @Override
    public int insertClueContactInfo(ClueContact clueContact) {
        return clueContactCustomMapper.insertClueContactInfo(clueContact);
    }

    /**
     * 线索下联系人
     * @param clueId
     * @return
     */
    @Override
    public List<ClueContact> selectClueContactList(String clueId) {
        ClueContactExample clueContactExample = new ClueContactExample();
        ClueContactExample.Criteria criteria = clueContactExample.createCriteria();
        criteria.andClueIdEqualTo(clueId);
        List<ClueContact> list = clueContactMapper.selectByExample(clueContactExample);
        return list;
    }

    /**
     * 根据线索id、联系人名称和联系方式判重，是否插入线索联系人
     * @Author pym
     * @Date  2021/3/4 13:49
     * @param clueId --线索id
     * @param contactName --联系人名称
     * @param contactInformation --联系方式
     * @return {@link {java.lang.Boolean}}
     */
    @Override
    public ClueContact isInsertClueContact(String clueId, String contactName, String contactInformation) {
        ClueContactExample clueContactExample = new ClueContactExample();
        ClueContactExample.Criteria criteria = clueContactExample.createCriteria();
        criteria.andClueIdEqualTo(clueId);
        criteria.andContactNameEqualTo(contactName);
        criteria.andContactInformationEqualTo(contactInformation);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<ClueContact> clueContacts = clueContactMapper.selectByExample(clueContactExample);
        if (clueContacts.size() > 0) {
            return clueContacts.get(0);
        }
        return null;
    }

    /**
     * 线索线上跟进判断联系人是否存在
     * @Author pym
     * @Date  2021/7/7 16:38
     * @param clueId 线索id
     * @param contactInformation 联系方式
     * @return {@link {com.crm.model.entity.cluePoolManager.ClueContact}}
     */
    @Override
    public ClueContact getClueContactByIdAndContactInfo(String clueId, String contactInformation) {
        ClueContactExample clueContactExample = new ClueContactExample();
        ClueContactExample.Criteria criteria = clueContactExample.createCriteria();
        criteria.andClueIdEqualTo(clueId);
        criteria.andContactInformationEqualTo(contactInformation);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<ClueContact> clueContacts = clueContactMapper.selectByExample(clueContactExample);
        if (clueContacts.size() > 0) {
            return clueContacts.get(0);
        }
        return null;
    }


}
